package com.vercon.mobilesafe74.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.vercon.mobilesafe74.R;
import com.vercon.mobilesafe74.util.ConstantValue;
import com.vercon.mobilesafe74.util.SpUtil;
import com.vercon.mobilesafe74.util.ToastUtil;


public class HomeActivity extends AppCompatActivity {

    private TextView mTextView;
    private GridView gv_home;
    private String[] mMTitleStrs;
    private int[] mDrawables;
    private EditText et_set_psd;
    private EditText et_confirm_psd;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        //1.初始化UI
        initUI();
        //2.初始化数据
        initDate();
    }

    private void initDate() {
        //1.准备数据
        mMTitleStrs = new String[]{"手机防盗","通信卫士","软件管理","进程管理","流量统计","手机杀毒","缓存清理","高级工具","设置中心"};
        mDrawables = new int[]{R.drawable.home_safe,R.drawable.home_callmsgsafe,
                R.drawable.home_apps,R.drawable.home_taskmanager,
                R.drawable.home_netmanager,R.drawable.home_trojan,
                R.drawable.home_sysoptimize,R.drawable.home_tools,R.drawable.home_settings};
        //九宫格控件设置数据适配器(等同ListView数据适配器)
        gv_home.setAdapter(new MyAdapter());
        //3.设置点击事件——这是设置的点击是可以选择的item
        gv_home.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        //开启对话框
                        showDialog();
                        break;
                    case 8:
                        Intent intent=new Intent(getApplicationContext(),SettingActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    private void showDialog() {
        //1.判断是否是首次开启对话框——根据是否存储
        String psd = SpUtil.getString(this, ConstantValue.MOBILE_SAFE_PSD, "");
        if (TextUtils.isEmpty(psd)){
            //设置初始化时的对话框
            showSetPsdDialog();
        }else {
            //确认密码对话框
            showConformPsdDialog();
        }
    }

    /**
     * 初始化时的对话框
     */
    private void showSetPsdDialog() {
        //1.因为对话框的样式是自己定义的，所以需要调用dialog.setView(view) view是xml转化的
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        //2.把自己编写的样式通过inflate转换成view,然后设置进去
        final View view = View.inflate(getApplicationContext(), R.layout.dialog_setpad, null);
        dialog.setView(view,0,0,0,0);//低版本的兼容问题
        dialog.show();

        //找到里面的控件(xml转化成view的时候，要用view去找我们关心的控件）
        Button btn_submit = view.findViewById(R.id.btn_submit);
        Button btn_cancel = view.findViewById(R.id.btn_cancel);
        //给事件设置点击按钮
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.先获取输入框里面的内容
                et_set_psd = view.findViewById(R.id.et_set_psd);
                et_confirm_psd = view.findViewById(R.id.et_confirm_psd);
                String set_text = et_set_psd.getText().toString();
                String confirm_text = et_confirm_psd.getText().toString();
                //2.先判断输入框是否为空
                if ((!TextUtils.isEmpty(confirm_text))&&!TextUtils.isEmpty(set_text)){
                    if (set_text.equals(confirm_text)){
                        //进入手机防盗模块（开启新一个activity）
                        Intent intent=new Intent(getApplicationContext(), SetupOverActivity.class);
                        startActivity(intent);
                        //跳转到新的界面以后需要去隐藏对话框
                        dialog.dismiss();
                        //2.1把第一次登陆的密码存储起来
                        SpUtil.putString(getApplicationContext(),ConstantValue.MOBILE_SAFE_PSD,set_text);
                    }else {
                        ToastUtil.show(getApplicationContext(),"密码输入不一致");
                    }
                }else {
                    //提醒用户密码为空
                    ToastUtil.show(getApplicationContext(),"输入框没有输入完整");
                }

            }
        });
        //点击取消按钮
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
    private void showConformPsdDialog() {
        //1.因为对话框的样式是自己定义的，所以需要调用dialog.setView(view) view是xml转化的
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        //2.把自己编写的样式通过inflate转换成view,然后设置进去
        final View view = View.inflate(getApplicationContext(), R.layout.dialog_confirmpad, null);
        dialog.setView(view);
        dialog.show();

        //找到里面的控件(xml转化成view的时候，要用view去找我们关心的控件）
        Button btn_submit = view.findViewById(R.id.btn_submit);
        Button btn_cancel = view.findViewById(R.id.btn_cancel);
        //给事件设置点击按钮
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.先获取输入框里面的内容
                et_set_psd = view.findViewById(R.id.et_set_psd);
                String set_text = et_set_psd.getText().toString();
                //2.先判断输入框是否为空
                if(!TextUtils.isEmpty(set_text)){
                    String psd = SpUtil.getString(getApplicationContext(), ConstantValue.MOBILE_SAFE_PSD, "");
                    if(psd.equals(set_text)){
                        //进入应用手机防盗模块,开启一个新的activity
                        Intent intent = new Intent(getApplicationContext(), SetupOverActivity.class);
                        startActivity(intent);
                        //跳转到新的界面以后需要去隐藏对话框
                        dialog.dismiss();
                    }else{
                        ToastUtil.show(getApplicationContext(),"确认密码错误");
                    }
                }else{
                    //提示用户密码输入有为空的情况
                    ToastUtil.show(getApplicationContext(), "请输入密码");
                }
            }
        });
        //点击取消按钮
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }




    private void initUI() {
        mTextView = findViewById(R.id.tv_news);
        mTextView.setSelected(true); // 设置为选中状态，以启动走马灯效果
        gv_home = findViewById(R.id.gv_home);
    }

    class MyAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return mMTitleStrs.length;
        }

        @Override
        public Object getItem(int position) {
            return mMTitleStrs[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //1.设置inflate,将子布局与视图绑定
            View view = View.inflate(getApplicationContext(), R.layout.gridview_item, null);
            ImageView iv_icon = view.findViewById(R.id.iv_icon);
            TextView iv_title = view.findViewById(R.id.iv_title);

            //2.把对应的值设置进去
            iv_title.setText(mMTitleStrs[position]);
            iv_icon.setImageResource(mDrawables[position]);
            return view;
        }
    }
}
